/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.dt.core.manager;

import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.entity.uri.MetadataURI;
import com.inspur.edp.sgf.api.common.SummaryInfo;
import com.inspur.edp.sgf.api.entity.ServiceOperation;
import com.inspur.edp.sgf.dt.core.cache.ResourceProviderCache;
import com.inspur.edp.sgf.dt.core.exception.ExceptionCode;
import com.inspur.edp.sgf.dt.spi.ResourceProvider;
import com.inspur.edp.sgf.utils.exception.EApiDesignException;

import javax.annotation.Nonnull;
import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;

/**
 * @author Fynn Qi
 * @author Kaixuan Shi
 * @version 1.1
 * @since 2020/12/23 11:45
 */
public class EapiProviderManager {

    public static EapiProviderManager instance;

    static {
        instance = new EapiProviderManager();
    }

    public List<SummaryInfo> getSummaryOperations(@Nonnull String resourceType, MetadataURI targetMdURI, MetadataURI sourceMdURI, String projectPath) {
        ResourceProvider provider = getResourceProvider(resourceType);
        return provider.getSummaryOperations(targetMdURI, sourceMdURI, projectPath);
    }

    public List<ServiceOperation> getOperations(String resourceType, MetadataURI targetMdURI, MetadataURI sourceMdURI, String projectPath) {
        ResourceProvider provider = getResourceProvider(resourceType);
        return provider.getOperations(targetMdURI, sourceMdURI, projectPath);
    }

    public List<ServiceOperation> getOperations(String resourceType, MetadataURI targetMdURI, MetadataURI sourceMdURI, String projectPath, List<String> operationIds) {
        ResourceProvider provider = getResourceProvider(resourceType);
        return provider.getOperations(targetMdURI, sourceMdURI, projectPath, operationIds);
    }

    public GspMetadata createEapi(GspMetadata metadata, String path, String resourceType) {
        ResourceProvider provider = getResourceProvider(resourceType);
        return provider.create(metadata, path);
    }

    public GspMetadata createEapi(GspMetadata metadata, String resourceType) {
        ResourceProvider provider = getResourceProvider(resourceType);
        return provider.create(metadata);
    }

    public GspMetadata updateEapi(String metadataId, GspMetadata metadata, String resourceType) {
        ResourceProvider provider = getResourceProvider(resourceType);
        return provider.update(metadataId, metadata);
    }

    /**
     * 获取资源定义适配器
     *
     * @param resourceType 资源类型
     */
    private ResourceProvider getResourceProvider(String resourceType) {
        ResourceProvider provider = ResourceProviderCache.getInstance().getResourceProvider(resourceType);
        if (Objects.isNull(provider)) {
            throw new EApiDesignException(ExceptionCode.GSP_EAPI_DESIGN_0101,
                    MessageFormat.format("获取{0}外部服务定义资源适配器失败。", resourceType));
        }
        return provider;
    }
}
